/*
 * ILinkConstants.java
 * 
 * Version information
 * 
 * Feb 20, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.ilink.util;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

/**
 * This class defines constants which are used in different classes through out
 * the ILink Service.
 * 
 */
public class ILinkConstants {

	private static final Logger oLog = Logger.getLogger(ILinkConstants.class);

	private static ResourceBundle bundle = ResourceBundle
			.getBundle("ilinkResources");

	public static final short STATE_PENDING = 0;

	public static final short STATE_ACTIVE = 1;

	public static final short STATE_BANNED = 2;

	public static final short STATE_INACTIVE = 3;

	// forwards
	public static final String FWD_INAME_PAGE = "iname";

	public static final String FWD_CHKLOGIN = "CheckLogin";

	public static final String FWD_SUCCESS = "success";

	public static final String FWD_FAILURE = "failure";

	public static final String FWD_ILINKS = "ilinks";

	public static final String FWD_PUBLIC = "index";

	// errors
	public static final String ERR_INVALID = "err.invalid";

	public static final String ERR_REQUIRED = "err.required";

	public static final String ERR_GLOBAL_LOGOUT_NOT_DONE = "err.global.logout.not.done";

	public static final String ERR_UKNOWN_GLOBAL_SESSION = "err.unknown.global.session";

	public static final String ERR_AUTHN_FAILED = "err.authn.failed";

	public static final String ERR_EXCEEDED_ATTEMPTS = "exeeded.attempts";

	public static final String ERR_INVALID_AUTHENTICATION = "err.invalid.authentication";

	public static final String ERR_INAME_UNRESOLVED = "iname.not.resolved";

	// Forms
	public static final String SESSION_LOGIN_OBJ = "LoginData";

	public static final String REQUEST_CONFIG_OBJ = "configData";

	public static final String SESSION_IBROKER_OBJ = "IbrokerData";

	// messages
	public static final String MSG_LOGIN_SUCCESS = "msg.login.success";

	public static final String MSG_LOGOUT_SUCCESS = "msg.logout.success";

	public static final String MSG_SAVE_SUCCESS = "msg.ilink.save.success";

	public static final String MSG_SAVE_FAIL = "msg.ilink.save.fail";

	public static final String MSG_UPDATE_SUCCESS = "msg.ilink.update.success";

	public static final String MSG_UPDATE_FAIL = "msg.ilink.update.fail";

	public static final String MSG_DELETE_SUCCESS = "msg.ilink.delete.success";

	public static final String MSG_DELETE_FAIL = "msg.ilink.delete.fail";

	public static final String ALLERT_SESSION_EXPIRE = "alert.session.expire";

	public static final String PUBLIC_TAG = "+index";

	// Dictionary
	public static final String DICTIONARY_PERSONAL_URL = bundle
			.getString("dictionary.personal.url");

	public static final String DICTIONARY_BUSINESS_URL = bundle
			.getString("dictionary.business.url");

	public static final String DICTIONARY_COMMUNITY_URL = bundle
			.getString("dictionary.community.url");

	// Request attributes
	public static final String REQ_ATTRIBUTE_STATUS = "status";

	public static final String REQ_ATTRIBUTE_OPTYPE = "opType";

	public static final String REQ_ATTRIBUTE_MESSAGE = "message";

	public static final String REQ_ATTRIBUTE_SCOPE = "scope";

	public static final String URL_XRI_NET = bundle.getString("xri.net.url");

	private static Hashtable<Integer, String> ohErrors = null;

	public static Hashtable<String, String> oIBrokerTable = new Hashtable<String, String>();

	// Requested Authentication Contexts and Comparison type
	public static final String RAC_NAMES_LIST = bundle
			.getString("rac.names.list");

	public static final String RAC_COMPARISON_TYPE = bundle
			.getString("rac.comparison.type");

	public static final void init() {
		if (ohErrors == null) {
			ohErrors = new Hashtable<Integer, String>();

			// user exception
			ohErrors.put(1001, "Password does not match");
			ohErrors.put(1000, " Email address is not valid");
			ohErrors.put(1005, " User could not be logged in");
			// ohErrors.put(1002, "Synonym does not exist");

			// system exceptions
			ohErrors.put(2000, "GlobalINumber already exists ");
			ohErrors.put(2001, "Synonym already exists");
			ohErrors.put(2003, "SEP is not in corect format");
			ohErrors.put(2006, "GlobalInumber does not exist");
			ohErrors.put(2007, "Account is not active");
			ohErrors.put(2010, "User does not belong to this IBroker");

			ohErrors.put(2012, "IBroker could not be  created");
			ohErrors.put(2004, "IBroker not provisioned");
			ohErrors.put(2014, "Invalid Fields");
			ohErrors.put(2016, "No permissions to insert the supplied Fields");
			ohErrors.put(2017, "Data Could not be deleted");
			ohErrors.put(2018,
					"Specified Operations was not performed successfully");
			ohErrors.put(2020, "No Synonyms Found");
			ohErrors
					.put(2021,
							"No permissions to update one or more of the specified fields");
			ohErrors.put(2022, "Data could not be updated");
			ohErrors.put(2023, "Synonyms could not be updated");
			ohErrors.put(2024, "Subscriber could not created");

			ohErrors.put(2028, "InSufficient Data");
			ohErrors.put(2030, "No matching record found ");
			ohErrors.put(2031, "IBroker already exist");
			ohErrors.put(2034, "Iname/Inumber can't be resolved");
			ohErrors.put(2036, "This Method is currently not implemented.");

			ohErrors.put(101, "Duplicate");
			ohErrors.put(102, "Parse Error");

		}
	}

	public static final String getError(int iErrorCode)
			throws NullPointerException {
		if (ohErrors == null) {
			init();
		}
		return (String) ohErrors.get(new Integer(iErrorCode));
	}

	public static final void initializeIbrokerData() {
		if (oIBrokerTable == null || oIBrokerTable.size() == 0) {
			ResourceBundle oBundle = ResourceBundle.getBundle("ibrokers");
			Enumeration oBundleKeys = oBundle.getKeys();
			while (oBundleKeys.hasMoreElements()) {
				String sIBname = (String) oBundleKeys.nextElement();
				oIBrokerTable.put(sIBname, oBundle.getString(sIBname));
			}
		}
	}

	public static final String getSPXri(String ibrokerName) {
		if (oIBrokerTable == null || !oIBrokerTable.containsKey(ibrokerName))
			initializeIbrokerData();

		String sXRI = (String) oIBrokerTable.get(ibrokerName);
		oLog.debug("getSPXri(): IBroker: " + ibrokerName + ", XRI: " + sXRI);
		return sXRI;
	}
}